<?php
require_once("db_functions.php");
require_once("page.class.php");
require_once("functions.php");

define("PAGE_MAX",20);

class artikli extends page{
	
	var $db;
	
	function artikli(){
		$this->db = new db_functions();
		$this->loadConfig();
		
		$a = getFromInput("a");
		$mod = getFromInput("mod");
		switch ($a){
			case 'oglas_koeficijent':
				switch ($mod){
					case "save":
						$this->OglasKoeficijentSave();
						break;
					default:
						$this->OglasKoeficijentBrowse();
						break;
				}
				break;
			case "artikli":
				switch ($mod){
					case "save":
						$this->artikliSave();
						break;
					case "delete":
						$this->artikliDelete();
						break;
					default:
						$this->artikliBrowse();
						break;
				}
				break;
			case "artikli_tipovi":
				switch ($mod){
					case "save":
						$this->artikliTipoviSave();
						break;
					case "delete":
						$this->artikliTipoviDelete();
						break;
					default:
						$this->artikliTipoviBrowse();
						break;
				}
				break;
			case "artikli_grupe":
				switch ($mod){
					case "save":
						$this->artikliGrupeSave();
						break;
					case "delete":
						$this->artikliGrupeDelete();
						break;
					default:
						$this->artikliGrupeBrowse();
						break;
				}
				break;
			case "artikli_boje":
				switch ($mod){
					case "save":
						$this->artikliBojeSave();
						break;
					case "delete":
						$this->artikliBojeDelete();
						break;
					default:
						$this->artikliBojeBrowse();
						break;
				}
				break;
			case "artikli_paketi":
				switch ($mod){
					case "new":
						$this->artikliPaketiNew();
						break;
					case "edit":
						$this->artikliPaketiEdit();
						break;
					case "reloadCene":
						$this->reloadPaketSadrzajCene();
						break;
					case "save":
						$this->artikliPaketiSave();
						break;
					case "delete":
						$this->artikliPaketiDelete();
						break;
					default:
						$this->artikliPaketiBrowse();
						break;
				}
				break;
			case "profili_paketi":
				switch ($mod){
					case "delete":
						$this->profiliPaketiDelete();
						break;
					case "save":
						$this->profiliPaketiSave();
						break;
					case "edit":
						$this->profilePaketiEdit();
						break;
					default:
						$this->profiliPaketiBrowse();
						break;
				}
				break;
			default:
				//echo "Unknown action: $a";
				break;
		}
	}
	
	function artikliBrowse(){
		$fArtname = trim(readValFromInput("filter_artname"));
		$fArtgroup= getFromInput("filter_artgroup");
		$page = getFromInput("page");
		$page = $page>0?$page:1;
		
		$this->assign("filter_artname",$fArtname);
		$this->assign("filter_artgroup",$fArtgroup);
		$polja = $this->get_ArtikliGrupe($fArtgroup);
			$this->assign("artgroup_id",$polja["polje_id"]);
			$this->assign("artgroup_name",$polja["polje_ime"]);
			$this->assign("artgroup_selected",$polja["polje_selected"]);
		
		$q = "SELECT a.*, ag.artgr_name FROM artikli a, artikli_grupe ag WHERE a.art_grupa_id=ag.artgr_id ";
		if (strlen($fArtname)>0) {
			$q .= "AND art_name LIKE '%$fArtname%' ";
		}
		if($fArtgroup>0){
			$q .= "AND art_grupa_id=$fArtgroup ";
		}
		$q .= "ORDER BY art_name";
		$res = $this->db->select2($q);
		if($res !== false){
			$ukupno = count($res);
			$this->assign("info","Ukupno: $ukupno");
			$num_pages = bcdiv($ukupno,PAGE_MAX,0);;
			if(($ukupno%PAGE_MAX)>0){$num_pages++;}
			
			$start = ($page-1)*PAGE_MAX;
			for ($i=1; $i<=$num_pages; $i++){
				$pages[] = $i;
				if ($i==$page) {
					$pages_current[] = "current";
				}else{
					$pages_current[] = "";
				}
			}
			$this->assign("page",$pages);
			$this->assign("current_page",$pages_current);
			
			$q .= " LIMIT $start,".PAGE_MAX;
			$res = $this->db->select2($q);
			foreach ($res as $row){
				$ids[] = $row["art_id"];
				$names[] = $row["art_name"];
				$groups[] = $row["artgr_name"];
				$group_ids[] = $row["art_grupa_id"];
				$descs[] = $row["art_desc"];
			}
			$this->assign("art_id",$ids);
			$this->assign("art_name",$names);
			$this->assign("art_group",$groups);
			$this->assign("art_group_id",$group_ids);
			$this->assign("art_desc",$descs);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema rezultata");
		}
		
		echo $this->parseTemplate("artikli/browse.tpl");
	}
	function artikliSave(){
		$id = getFromInput("edit_art_id");
		$nname = trim(readValFromInput("edit_art_name"));
		$ngroup = getFromInput("edit_art_group");
		$ndesc = trim(readValFromInput("edit_art_desc"));
		
		if ($id>0) {
			$q = "UPDATE artikli SET "
				."art_name='".addslashes($nname)."',"
				."art_grupa_id=$ngroup,"
				."art_desc='".addslashes($ndesc)."' "
				."WHERE art_id=$id LIMIT 1";
			$msg = "Izmene sačuvane.";
		}else{
			$q = "INSERT INTO artikli(art_name,art_grupa_id,art_desc) VALUES("
				."'".addslashes($nname)."',$ngroup,'".addslashes($ndesc)."')";
			$msg = "Artikl uspešno dodat.";
		}
		
		if($this->db->alterTable($q)){
			echo $msg;
		}else{
			echo mysql_error();
		}
	}
	function artikliDelete(){
		$id = getFromInput("id");
		if ($this->db->alterTable("DELETE FROM artikli WHERE art_id=$id LIMIT 1")) {
			echo "Artikl obrisan.";
		}else{
			echo mysql_error();
		}
	}
	
	function artikliTipoviBrowse(){
		$polja = $this->get_ArtikliTipovi();
			$this->assign("type_id",$polja["polje_id"]);
			$this->assign("type_name",$polja["polje_ime"]);
		
		echo $this->parseTemplate("artikli/types.tpl");
	}
	function artikliTipoviSave(){
		$id = trim(getFromInput("id"));
		$name = readValFromInput("name");
		if (preg_match('/^\d+$/',$id)) {
			$q = "UPDATE artikli_tipovi SET arttip_name='".addslashes($name)."' WHERE arttip_id=$id LIMIT 1";
			$msg = "Tip uspešno izmenjen.";
		}else{
			$q = "INSERT INTO artikli_tipovi(arttip_name) VALUES('".addslashes($name)."')";
			$msg = "Tip uspešno dodat.";
		}
		if ($this->db->alterTable($q)) {
			echo $msg;
		}else{
			echo mysql_error();
		}
	}
	function artikliTipoviDelete(){
		$id = getFromInput("id");
		if($this->db->alterTable("DELETE FROM artikli_tipovi WHERE arttip_id=$id LIMIT 1")){
			echo "Tip uspešno obrisan.";
		}else{
			echo mysql_error();
		}
	}
	
	function artikliGrupeBrowse(){
		
		$polja = $this->get_ArtikliGrupe();
			$this->assign("group_id",$polja["polje_id"]);
			$this->assign("group_name",$polja["polje_ime"]);
		
		echo $this->parseTemplate("artikli/groups.tpl");
	}
	function artikliGrupeSave(){
		$id = getFromInput("id");
		$name = readValFromInput("name");
		if ($id>0) {
			$q = "UPDATE artikli_grupe SET artgr_name='".addslashes($name)."' WHERE artgr_id=$id LIMIT 1";
			$msg = "Grupa uspešno izmenjena.";
		}else{
			$q = "INSERT INTO artikli_grupe(artgr_name) VALUES('".addslashes($name)."')";
			$msg = "Grupa uspešno dodata.";
		}
		
		if ($this->db->alterTable($q)) {
			echo $msg;
		}else{
			echo mysql_error();
		}
	}
	function artikliGrupeDelete(){
		$id = getFromInput("id");
		if($this->db->alterTable("DELETE FROM artikli_grupe WHERE artgr_id=$id LIMIT 1")){
			echo "Grupa uspešno obrisana.";
		}else{
			echo mysql_error();
		}
	}
	
	function artikliBojeBrowse(){
		$polja = $this->get_ArtikliBoje();
			$this->assign("color_id",$polja["polje_id"]);
			$this->assign("color_name",$polja["polje_ime"]);
			$this->assign("color_code",$polja["polje_code"]);
		
		echo $this->parseTemplate("artikli/colors.tpl");
	}
	function artikliBojeSave(){
		$id		= getFromInput("id");
		$name	= readValFromInput("name");
		$code	= readValFromInput("code");
		
		if (preg_match('/^\d+$/',$id)) {
			$q = "UPDATE artikli_boje SET "
				 ."art_boja_name='".addslashes($name)."', "
				 ."art_boja_code='".addslashes($code)."' "
				 ."WHERE art_boja_id=$id LIMIT 1";
			$msg = "Boja uspešno izmenjena.";
		}else {
			$q = "INSERT INTO artikli_boje(art_boja_name,art_boja_code) VALUES('".addslashes($name)."','".addslashes($code)."')";
			$msg = "Boja uspešno dodata.";
		}
		if ($this->db->alterTable($q)) {
			echo $msg;
		}else{
			echo mysql_error();
		}
	}
	function artikliBojeDelete(){
		$id = getFromInput("id");
		if($this->db->alterTable("DELETE FROM artikli_boje WHERE art_boja_id=$id LIMIT 1")){
			echo "Boja uspešno obrisana.";
		}else{
			echo mysql_error();
		}
	}
	
	function artikliPaketiBrowse(){
		$fName = trim(readValFromInput("filter_name"));
		$fDateStart = trim(getFromInput("filter_date_start"));
		$fDateEnd = trim(getFromInput("filter_date_end"));
		$page = getFromInput("page");
		
		if (!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDateStart)) {
			$fDateStart = "";
		}
		if (!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDateEnd)) {
			$fDateEnd="";
		}
		/*if ($fDateStart=="" && $fDateEnd=="" && strlen($fName)==0) {
			$prevMonth = intval(date("m"))-1; $prevMonth = $prevMonth<10?"0$prevMonth":$prevMonth;
			$year = date("Y");
			$fDateStart = "01.$prevMonth.$year";
		}*/
		
		$this->assign("filter_name",$fName);
		$this->assign("filter_date_start",$fDateStart);
		$this->assign("filter_date_end",$fDateEnd);
		
		$fTimeStart = date2int($fDateStart);
		$fTimeEnd	= date2int($fDateEnd);
		
		$q = "SELECT ap.*, c.cena_art_id, a.art_name, at.arttip_name, ab.art_boja_code, c.cena_art_mera, jm.unit_svalue, c.cena_id, c.cena_value, apa.popust "
			."FROM artikli a, artikli_paketi ap, artikli_paketi_artikli apa, cene c, artikli_tipovi at, artikli_boje ab, jedinice_mere jm "
			."WHERE ap.paketid=apa.paket "
			."AND a.art_id=c.cena_art_id "
			."AND c.cena_art_tip=at.arttip_id "
			."AND c.cena_art_boja=ab.art_boja_id "
			."AND c.cena_art_jed_mere=jm.unit_id "
			."AND apa.cena=c.cena_id ";
		if(strlen($fName)){
			$q .="AND ap.paketname LIKE '%$fName%' ";
		}
		if ($fTimeStart>0) {
			if ($fTimeEnd>0) {
				$q .= "AND ap.paket_start>=$fTimeStart AND ap.paket_end<=$fTimeEnd ";
			}else{
				$q .= "AND ap.paket_start>=$fTimeStart ";
			}
		}elseif ($fTimeEnd>0) {
			$q .= "AND ap.paket_end<=$fTimeEnd ";
		}
		$q .= "ORDER BY ap.paket_start DESC, ap.paket_end DESC, a.art_name ASC";
		//echo $q;
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $row){
				$data[$row["paketid"]]["name"] = $row["paketname"];
				$data[$row["paketid"]]["start"] = $row["paket_start"];
				$data[$row["paketid"]]["end"] = $row["paket_end"];
				$data[$row["paketid"]]["cena"] = $row["cena"];
				$data[$row["paketid"]]["opis"] = $row["opis"];
				$data[$row["paketid"]]["artikli"][] = array(
					"id"	=> $row["cena_id"],
					"art_id"=> $row["cena_art_id"],
					"tip"	=> $row["arttip_name"],
					"boja"	=> $row["art_boja_code"],
					"mera"	=> $row["cena_art_mera"],
					"jm"	=> $row["unit_svalue"],
					"name"	=> $row["art_name"],
					"cena"	=> $row["cena_value"],
					"popust"=> $row["popust"]
				);
			}
			
			foreach ($data as $paketid=>$row){
				
				$q = "SELECT cenovnik_id "
					."FROM cenovnici "
					."WHERE cenovnik_start<=".$row["start"]." "
					."ORDER BY cenovnik_start DESC "
					."LIMIT 1";
				$cenovnikLast = $this->db->getOne($q);
				
				$ids[] = $paketid;
				$names[] = $row["name"];
				$times[] = date("d.m.Y",$row["start"])." - ".date("d.m.Y",$row["end"]);
				$descs[] = nl2br($row["opis"]);
				
				$prices[] = number_format($row["cena"],2,",",".");
				$uk_cena=0;
				$a="<ul>";
				foreach ($row["artikli"] as $art){
					$c = floatval($art["cena"]);
					$popust = floatval($art["popust"]);
					//$c = $c-floatval($art["popust"]);
					$uk_cena += $c-$popust;
					
					$popust_perc = round($popust*100/$c,0);
					
					$a .= '<li>';
					$a .= '<span class="pack_art_price" title="popust: '.number_format($popust,2,",",".")." (".$popust_perc.'%)">'.number_format($c-$popust,2,",",".").'</span>';
					$a .= '<span class="pack_art_id">'.$art["art_id"].'.</span>';
					$a .= '<span class="pack_art_name">'.$art["name"];
					if (strlen(trim($art["tip"]))) {$a .= " / ".$art["tip"];}
					if (strlen(trim($art["boja"]))) {$a .= " / ".$art["boja"];}
					if ($art["mera"]>0) {$a .= " / ".$art["mera"]." ".strtolower($art["jm"]).".";}
					$a .= '</span>';
					$a .= '</li>';
				}
				$a .= "</ul>";
				$artikli[] = $a;
			}
			$this->assign("pack_id",$ids);
			$this->assign("pack_name",$names);
			$this->assign("pack_time",$times);
			$this->assign("pack_price",$prices);
			$this->assign("pack_desc",$descs);
			$this->assign("pack_arts",$artikli);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema rezultata.");
		}
		
		echo $this->parseTemplate("artikli/packages.tpl");
	}
	function artikliPaketiNew(){
		$this->assign("pakart_id",array("0"));
		$this->assign("pakart_popust",array("0"));
		echo $this->parseTemplate("artikli/package_edit.tpl");
	}
	function artikliPaketiEdit(){
		$id = getFromInput("id");
		$this->assign("paket_id",$id);
		
		/*$q = "SELECT cenovnik_id FROM artikli_paketi ap, cenovnici cen, cene c, artikli_paketi_artikli apa "
			."WHERE ap.paketid=apa.paket "
			."AND apa.cena=c.cena_id "
			."AND c.cena_cenovnik=cen.cenovnik_id "
			."ORDER BY cenovnik_start DESC LIMIT 1";
		$cenovnik = $this->db->getOne($q);*/
		
		$q = "SELECT cenovnik_id, cenovnik_start "
			."FROM cenovnici c, artikli_paketi ap "
			."WHERE c.cenovnik_start<=ap.paket_start "
			."AND ap.paketid=$id "
			."ORDER BY c.cenovnik_start DESC LIMIT 1";
		list($cenovnik,$cenovnik_start) = $this->db->getRow($q);
		
		$this->assign("cenovnik_datum",date("d.m.Y",$cenovnik_start));
		$cene = $this->get_Cene($cenovnik);
		
		$paket = $this->get_ArtikliPaketSadrzaj($id);
		
		if($paket !== false){
			$this->assign("paket_name",$paket["name"]);
			$this->assign("paket_start",date($this->getFromConfig("short_date"),$paket["start"]));
			$this->assign("paket_end",date($this->getFromConfig("short_date"),$paket["end"]));
			$this->assign("paket_price",number_format($paket["price"],2,",","."));
			
			$ids=$names=$cenids=$cen=$popust=array();
			foreach ($paket["artikli"] as $art){
				$ids[]		= $art["id"];
				$names		= $art["name"];
				$cenids[]	= $art["cena_id"];
				$cen[]		= $art["cena"];
				$iz = floatval($art["cena"])-(floatval($art["cena"])*floatval($art["popust"])/100);
				$iznos[]	= number_format($iz,2,",",".");
				$iznos_f[]	= $iz;
				$popust[]	= $art["popust"];
				$options[]	= $this->get_PaketCeneOptions($cene,$art["cena_id"]);
			}
			$this->assign("pakart_id",$ids);
			$this->assign("pakart_cene_options",$options);
			$this->assign("pakart_popust",$popust);
			$this->assign("pakart_iznos",$iznos);
			$this->assign("pakart_iznos_float",$iznos_f);
		}
		
		echo $this->parseTemplate("artikli/package_edit.tpl");
	}
	function reloadPaketSadrzajCene(){
		$date = getFromInput("datum");
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$date)) {
			$time = date2int($date);
			list($cenovnik,$cenovnik_start) = $this->db->getRow("SELECT cenovnik_id,cenovnik_start FROM cenovnici WHERE cenovnik_start<=$time ORDER BY cenovnik_start DESC LIMIT 1");
			$cene = $this->get_Cene($cenovnik);
			echo date("d.m.Y",$cenovnik_start)."{!}";
			echo $this->get_PaketCeneOptions($cene);
		}
	}
	function artikliPaketiSave(){
		$data = array();
		foreach ($_POST as $k=>$v){
			$v = utf8RawUrlDecode($v);
			$v = utf8RawChrDecode($v);
			if (preg_match('/^paket_cena_/',$k)) {
				$v = strip_number_format($v);
				$indx = str_replace("paket_cena_","",$k);
				$data["sadrzaj"][$indx]["cena"] = $v;
			}else if (preg_match('/^paket_popust_/',$k)) {
				$indx = str_replace("paket_popust_","",$k);
				$v = strip_number_format($v);
				$data["sadrzaj"][$indx]["popust"] = $v;
			}else{
				$data[$k] = $v;
			}
		}
		
		$paket_start = date2int($data["paket_start"]);
		$paket_end = date2int($data["paket_end"]);
		
		if (!strlen($data["paket_name"]) || !strlen($data["paket_cena"]) || $paket_start<1 || $paket_end<1 || !count($data["sadrzaj"])) {
			die("0{!}Niste popunili obavezna polja.");
		}
		
		if ($data["paket_id"]>0) {
			
			$q = "UPDATE artikli_paketi SET "
				."paketname='".addslashes($data["paket_name"])."',"
				."cena='".strip_number_format($data["paket_cena"])."',"
				."paket_start=$paket_start,"
				."paket_end='$paket_end',"
				."opis='".addslashes($data["paket_opis"])."' "
				."WHERE paketid=".$data["paket_id"]." LIMIT 1";
		}else{
			$q = "INSERT INTO artikli_paketi(paketname, cena, paket_start, paket_end, opis) "
				."VALUES('".addslashes($data["paket_name"])."','".strip_number_format($data["paket_cena"])."','".date2int($data["paket_start"])."','".date2int($data["paket_end"])."','".$data["paket_opis"]."')";
		}
		if ($this->db->alterTable($q)) {
			if ($data["paket_id"]>0) {
				if (!$this->db->alterTable("DELETE FROM artikli_paketi_artikli WHERE paket=".$data["paket_id"])) {
					die("0{!}".mysql_error());
				}
			}else{
				$data["paket_id"]=$this->db->getOne("SELECT LAST_INSERT_ID() FROM artikli_paketi LIMIT 1");
			}
				
			$msg = "1{!}Paket sačuvan";
			foreach ($data["sadrzaj"] as $pakart){
				$q = "INSERT INTO artikli_paketi_artikli(paket,cena,popust) VALUES('".$data["paket_id"]."','".$pakart["cena"]."','".$pakart["popust"]."')";
				if (!$this->db->alterTable($q)) {
					$msg = "0{!}".mysql_error();
					break;
				}
			}
			echo $msg;
			
		}else{
			die("0{!}".mysql_error()."\n$q");
		}
		
	}
	function artikliPaketiDelete(){
		$id = getFromInput("id");
		$q = "DELETE FROM artikli_paketi WHERE paketid=$id LIMIT 1";
		$q1= "DELETE FROM artikli_paketi_artikli WHERE paket=$id";
		if ($this->db->alterTable($q) && $this->db->alterTable($q1)) {
			echo "Paket uspešno obrisan";
		}else{
			echo mysql_error();
		}
	}
	
	
	function profiliPaketiBrowse(){
		
		$fName = readValFromInput("filter_name");
		$fDate = getFromInput("filter_date");
		$fDateTime = date2int($fDate);
		$fProfileType = getFromInput("filter_profile_type");
		$fPrice = getFromInput("filter_price");
		
		$fPrice = floatval($fPrice);
		
		$this->assign("filter_name",$fName);
		$this->assign("filter_date",$fDate);
		$this->assign("filter_price",$fPrice);
		
		require_once("classes/cenovnik.class.php");
		$polja = cenovnik::get_Artikli();
			$this->assign("art_id",$polja["polje_id"]);
			$this->assign("art_name",$polja["polje_ime"]);
			$this->assign("art_selected",$polja["polje_selected"]);
		
		require_once("classes/komitent.core.class.php");
		$polja = komitentObject::get_ProfiliTipovi($fProfileType);
			$this->assign("profile_type_id",$polja["id"]);
			$this->assign("profile_type_code",$polja["name"]);
			$this->assign("profile_type_selected",$polja["selected"]);
		
		$items = array();
		foreach ($_POST as $k=>$v){
			if (preg_match('/^filter_item_/',$k) && strtolower($v)=="on") {
				$items[] = intval(preg_replace('/filter_item_/',"",$k));
			}
		}
		$polja = $this->get_ProfiliItems($items);
			$this->assign("item_id",$polja["polje_id"]);
			$this->assign("item_name",$polja["polje_ime"]);
			$this->assign("item_selected",$polja["polje_selected"]);
			
		
		$q = "SELECT * FROM profili_paketi WHERE 1";
		
		if (strlen($fName)>0) {
			$q .= " AND name LIKE '%$fName%'";
		}
		if ($fDateTime>0) {
			$q .= " AND date_start>=$fDateTime";
		}
		if ($fProfileType!="" && $fProfileType!="null") {
			$q .= " AND profile_type=$fProfileType";
		}
		if (count($items)>0) {
			$q .= " AND (0";
			foreach ($items as $itemid){
				$q .= " OR items LIKE '%;$itemid;%'";
			}
			$q .= ")";
		}
		if ($fPrice>0) {
			$q .= " AND cena='$fPrice'";
		}
		$q .= " ORDER BY date_start DESC";
		$res = $this->db->select2($q);
		if ($res != false) {
			foreach ($res as $k=>$row){
				$ids[$k]	= $row["id"];
				$arts[$k]	= $this->db->getOne("SELECT art_name FROM artikli WHERE art_id=".$row["art_id"]." LIMIT 1");
				$profs[$k]	= $this->db->getOne("SELECT code FROM profili_tipovi WHERE id=".$row["profile_type"]." LIMIT 1");
				$names[$k]	= $row["name"];
				
				$itms = explode(";",$row["items"]);
				$content = "";
				foreach ($itms as $itm){
					if ($itm>0) {
						list($n,$p) = $this->db->getRow("SELECT name, cena FROM profili_items WHERE id=$itm LIMIT 1");
						$p = number_format($p,2,",",".");
						$content .= "<span class=\"prof_items_price\">$p</span>$n<br clear=\"all\" />";
					}
				}
				$items[$k]	= $content;
				$dates[$k]	= int2date($row["date_start"]);
				$cene[$k]	= number_format($row["cena"],2,",",".");
			}
			$this->assign("id",$ids);
			$this->assign("art",$arts);
			$this->assign("profile",$profs);
			$this->assign("name",$names);
			$this->assign("items",$items);
			$this->assign("date",$dates);
			$this->assign("price",$cene);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema paketa profila");
		}
		echo $this->parseTemplate("artikli/profili_paketi.tpl");
	}
	function profiliPaketiDelete(){
		
		$id = getFromInput("id");
		if($id>0){
			$q = "DELETE FROM profili_paketi WHERE id=$id LIMIT 1";
			if (!$this->db->alterTable($q)) {
				echo "1{!}".mysql_error();
			}else{
				echo "0{!}Paket obrisan";
			}
		}
	}
	function profiliPaketiSave(){
		$id = getFromInput("id");
		$artid = getFromInput("art_id");
		$profile_type = getFromInput("profile_type");
		$name = readValFromInput("name");
		$items = getFromInput("items");
		$date_start = date2int(getFromInput("date_start",true));
		$cena = floatval(strip_number_format(getFromInput("cena",true)));
		
		if(!strlen($name) || !strlen($profile_type) || !$artid || !$date_start || !$cena){
			die("1{!}Morate popuniti sva polja");
		}
		
		if($id>0){
			$q = "UPDATE profili_paketi SET "
				."art_id=$artid,"
				."profile_type=$profile_type,"
				."name='".addslashes($name)."',"
				."items='$items',"
				."date_start=$date_start,"
				."cena='$cena' "
				."WHERE id=$id LIMIT 1";
			//die("1{!}$q");
		}else{
			$q = "INSERT INTO profili_paketi VALUES('',"
			."$artid,"
			."$profile_type,"
			."'".addslashes($name)."',"
			."'$items',"
			."$date_start,"
			."'$cena'"
			.")";
		}
		
		if(!$this->db->alterTable($q)){
			echo "1{!}".mysql_error();
		}else{
			echo "0{!}Paket je sačuvan.";
		}
		
	}
	function profilePaketiEdit(){
		$id = getFromInput("id");
		$this->assign("id",$id);
		
		$paket = $this->get_ProfilPaket($id);
		$this->assign("name",$paket["name"]);
		$this->assign("date_start",int2date($paket["date_start"]));
		$this->assign("price",number_format($paket["cena"],2,",","."));
		$this->assign("items_raw",$paket["items_raw"]);
		
		require_once("classes/komitent.core.class.php");
		$polja = komitentObject::get_ProfiliTipovi($paket["profile_type"]);
			$this->assign("profile_type_id",$polja["id"]);
			$this->assign("profile_type_code",$polja["name"]);
			$this->assign("profile_type_selected",$polja["selected"]);
		$polja = $this->get_Artikli($paket["art_id"]);
			$this->assign("art_id",$polja["polje_id"]);
			$this->assign("art_name",$polja["polje_ime"]);
			$this->assign("art_selected",$polja["polje_selected"]);
			
		foreach ($paket["items"] as $k=>$v){
			$polja = $this->get_ProfiliItems();
			$select[$k] = '<select>';
			$item_ids[$k] = $v["id"];
			foreach ($polja["polje_id"] as $m=>$id){
				$select[$k] .= '<option value="'.$id.'"';
				if ($id==$v["id"]) {
					$select[$k] .= " selected";
				}
				$select[$k] .= '>'.$polja["polje_ime"][$m].'</option>';
			}
			$select[$k] .= "</select>";
		}
		$this->assign("item_id",$item_ids);
		$this->assign("item_select",$select);
		
		echo $this->parseTemplate("artikli/profili_paketi_edit.tpl");
	}
	
	function OglasKoeficijentBrowse(){
		
		$q = "SELECT a.art_id, a.art_name, a.art_desc, ask.koeficijent "
			."FROM artikli a LEFT JOIN artikli_strana_koeficijent ask ON a.art_id=ask.art_id "
			."WHERE a.art_grupa_id=1 "
			."ORDER BY a.art_name";
		$res = $this->db->select2($q);
		
		if ($res !== false) {
			foreach ($res as $k=>$row){
				$art_id[$k] = $row["art_id"];
				$art_name[$k] = $row["art_name"];
				$art_desc[$k] = $row["art_desc"];
				$koeficijent_f[$k] = $row["koeficijent"];
				$koeficijent[$k] = number_format($row["koeficijent"],4,",",".");
				$class[$k] = $row["koeficijent"]>0?"normal":"nula";
			}
			$this->assign("art_id",$art_id);
			$this->assign("art_name",$art_name);
			$this->assign("art_desc",$art_desc);
			$this->assign("koeficijent",$koeficijent);
			$this->assign("koeficijent_f",$koeficijent_f);
			$this->assign("class",$class);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema artikala u grupi PUBLIKACIJA.");
		}
		
		
		echo $this->parseTemplate("artikli/oglas_koeficijent.tpl");
	}
	
	function OglasKoeficijentSave(){
		$code = 0;
		$msg = "Odnos artikl-strana je sačuvan";
		
		$artid = getFromInput("artid");
		$koefc = getFromInput("k");
		
		$q = "INSERT INTO artikli_strana_koeficijent(art_id,koeficijent) VALUES($artid,'$koefc') "
			."ON DUPLICATE KEY UPDATE koeficijent='$koefc'";
		if (!@$this->db->alterTable($q)) {
			$code = 1;
			$msg = "Greška!\n\n".mysql_error();
		}
		
		echo "$code{!}$msg";
	}
	
	
	//-------------------------  GET FUNCS.
	function get_Artikli($artid=null,$group=null){
		$ret = array();
		if ($group!==null) {
			$where = " WHERE art_grupa_id=$group";
		}
		$q = "SELECT * FROM artikli$where ORDER BY art_name";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["art_id"];
				$ret["polje_ime"][$k] = $v["art_name"];
				$ret["polje_desc"][$k] = $v["art_desc"];
				$ret["polje_grupa"][$k] = $v["art_grupa_id"];
				$ret["polje_selected"][$k] = $v["art_id"]==$artid?" selected":"";
			}
		}
		return $ret;
	}
	function get_ArtikliGrupe($group=null){
		$ret = array();
		$res = $this->db->select2("SELECT * FROM artikli_grupe ORDER BY artgr_name");
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["artgr_id"];
				$ret["polje_ime"][$k] = $v["artgr_name"];
				$ret["polje_selected"][$k] = ($v["artgr_id"]==$group)?" selected":"";
			}
		}
		return $ret;
	}
	function get_ArtikliTipovi($type=null){
		$ret = array();
		$res = $this->db->select2("SELECT * FROM artikli_tipovi ORDER BY arttip_name");
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["arttip_id"];
				$ret["polje_ime"][$k] = $v["arttip_name"];
				$ret["polje_selected"][$k] = ($v["arttip_id"]==$type)?" selected":"";
			}
		}
		return $ret;
	}
	function get_Artikliboje($color=null){
		$ret = array();
		$res = $this->db->select2("SELECT * FROM artikli_boje ORDER BY art_boja_name");
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["art_boja_id"];
				$ret["polje_ime"][$k] = $v["art_boja_name"];
				$ret["polje_code"][$k] = $v["art_boja_code"];
				$ret["polje_selected"][$k] = ($v["art_boja_id"]==$color)?" selected":"";
			}
		}
		return $ret;
	}
	
	function get_ArtikliPaketSadrzaj($id=null){
		$ret = false;
		if ($id===null) {
			return false;
		}
		
		$q = "SELECT ap.*, c.cena_art_id, a.art_name, a.art_grupa_id, c.cena_id, c.cena_value, apa.popust "
			."FROM artikli a, artikli_paketi ap, artikli_paketi_artikli apa, cene c "
			."WHERE ap.paketid=apa.paket "
			."AND a.art_id=c.cena_art_id "
			."AND apa.cena=c.cena_id "
			."AND ap.paketid=$id "
			."ORDER BY art_name";
		$res = $this->db->select2($q);
		if ($res !== false) {
			$ret = array();
			foreach ($res as $row){
				$ret["name"]	= $row["paketname"];
				$ret["price"]	= $row["cena"];
				$ret["start"]	= $row["paket_start"];
				$ret["end"]		= $row["paket_end"];
				$ret["desc"]	= $row["opis"];
				
				$ret["artikli"][] = array(
					"id"		=> $row["cena_art_id"],
					"name"		=> $row["art_name"],
					"group"		=> $row["art_grupa_id"],
					"cena_id"	=> $row["cena_id"],
					"cena"		=> $row["cena_value"],
					"popust"	=> $row["popust"]
				);
			}
		}
		
		return $ret;
	}
	function get_Cene($cenovnik,$cenaid=null){
		$ret=array();
		$q = "SELECT c.*, art_name, arttip_name, art_boja_name, art_boja_code, unit_value, unit_svalue "
			."FROM cene c, artikli a, artikli_tipovi at, artikli_boje ab, jedinice_mere jm "
			."WHERE c.cena_art_id=a.art_id "
			."AND c.cena_art_tip=at.arttip_id "
			."AND c.cena_art_boja=ab.art_boja_id "
			."AND c.cena_art_jed_mere=jm.unit_id "
			."AND c.cena_cenovnik=$cenovnik ";
		
		$q .= "ORDER BY art_name ASC, c.cena_value DESC";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["cena_id"];
				$ret["polje_art_id"][$k] = $v["cena_art_id"];
				$ret["polje_art_name"][$k] = $v["art_name"];
				$ret["polje_tip_id"][$k] = $v["cena_art_tip"];
				$ret["polje_tip_name"][$k] = $v["arttip_name"];
				$ret["polje_boja_id"][$k] = $v["cena_art_boja"];
				$ret["polje_boja_name"][$k] = $v["art_boja_name"];
				$ret["polje_boja_code"][$k] = $v["art_boja_code"];
				$ret["polje_mera"][$k] = $v["cena_art_mera"];
				$ret["polje_jm_id"][$k] = $v["cena_art_jed_mere"];
				$ret["polje_jm_name"][$k] = $v["unit_value"];
				$ret["polje_jm_code"][$k] = $v["unit_svalue"];
				$ret["polje_value"][$k] = $v["cena_value"];
				if ($v["cena_id"]==$cenaid) {
					$ret["polje_selected"][$k] = " selected";
				}else{
					$ret["polje_selected"][$k] = "";
				}
			}
		}
		return $ret;
	}
	function get_PaketCeneOptions($cene,$cid){
		$ret = "";
		if(count($cene)){
			foreach ($cene["polje_id"] as $k=>$v){
				
				$oVal = $v;
				$oCen = $cene["polje_value"][$k];
				$oArt = $cene["polje_art_name"][$k];
				if ($cene["polje_tip_id"][$k]>0) {
					$oArt .= " - ".$cene["polje_tip_name"][$k];
				}
				if ($cene["polje_boja_id"][$k]>0) {
					$oArt .= " - ".$cene["polje_boja_code"][$k];
				}
				if ($cene["polje_mera"][$k]>0) {
					$oArt .= " - ".$cene["polje_mera"][$k]."".strtolower($cene["polje_jm_code"][$k]).".";
				}
				if ($v==$cid) {
					$oSelected = " selected";
				}else{
					$oSelected = "";
				}
				
				$ret .= "\n\t".'<option value="'.$oVal.'" cena="'.$oCen.'" title="Cena: '.number_format($oCen,2,",",".").'"'.$oSelected.'>'.$oArt.'</option>';
			}
		}
		return $ret;
	}
	
	function get_ProfilPaket($id){
		$res = $this->db->getRow("SELECT * FROM profili_paketi WHERE id=$id LIMIT 1",true);
		$ret = array();
		if ($res) {
			$ret = $res;
			$ret["items_raw"] = $ret["items"];
			$items = explode(";",$ret["items"]);
			$ret["items"] = array();
			foreach ($items as $row){
				if (strlen(trim($row))) {
					$key = count($ret["items"]);
					$ret["items"][$key]["id"] = $row;
					$ret["items"][$key]["name"] = $this->db->getOne("SELECT name FROM profili_items WHERE id=$row LIMIT 1");
				}
			}
		}
		return $ret;
	}
	function get_ProfiliItems($items=array()){
		$ret = false;
		$res = $this->db->select2("SELECT * FROM profili_items ORDER BY name");
		if ($res) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_ime"][$k] = $v["name"];
				$ret["polje_query"][$k] = $v["query"];
				$ret["polje_cena"][$k] = $v["cena"];
				if (in_array($v["id"],$items)) {
					$ret["polje_selected"][$k] = " checked";
				}else{
					$ret["polje_selected"][$k] = "";
				}
			}
		}
		return $ret;
	}
	
}

?>